package cn.edu.cqvie.netty.grpc;

import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;

public class GrpcServer {


    private Server server;

    private void start() {
        try {
            this.server = ServerBuilder
                    .forPort(8899)
                    .addService(new StudentServiceImpl())
                    .build().start();

            System.out.println("server started!");

            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    System.err.println("关闭 JVM");
                    GrpcServer.this.stop();
                }
            });

            System.out.println("执行到这里");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void stop () {
        if (null != this.server) {
            this.server.shutdown();
        }
    }

    private void awaitTermination() throws InterruptedException {
        if (null != this.server) {
            this.server.awaitTermination();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        GrpcServer server = new GrpcServer();
        server.start();
        server.awaitTermination();
    }
}
